--
-- Reading/writing DOT format
--
-- Author:  Nikita Afanasenko <nikita@afanasenko.name>
-- Corrector: Dmitry Korolev
-- License: GPL
--

module DotCover where

import Data.Graph.Inductive.Example(labUEdges)
import Data.Graph.Inductive.Tree(UGr)
import Data.Graph.Inductive.Graph(Node, Edge, LNode, LEdge, mkGraph, nodes, edges)

import Text.Regex.Posix
import Debug.Trace

import Data.Set(member, Set)

showGraph :: UGr -> [Node] -> String
showGraph g cover =
  concat $
    "graph G {":
      [if node `elem` cover
        then show node ++ " [label=\"cover\", shape=\"box\"];\n"
        else show node ++ ";\n"
      | node <- nodes g] ++
      "\n":
      [show f ++ " -- " ++ show s ++ ";\n" | (f,s) <- edges g] ++
    ["}"]

-- This is a rather simplified way to parse DOT format
readDot :: String -> UGr
readDot str =
  let edgesPattern = "^[ |\t]*([0-9]+) *-- *([0-9]+);"
      nodesPattern = "^[ |\t]*([0-9]+);"
      nodes = str =~ nodesPattern :: [[String]]
      edges = str =~ edgesPattern :: [[String]]
  in
  mkGraph
    [(read e, ()) | _:e:_ <- nodes]
    [(read l, read r, ()) | _:l:r:_ <- edges]

